﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Rendering.AsyncRequestNativeArrayData
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Bindings;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine.Rendering
{
  [UsedByNativeCode]
  [NativeHeader("Runtime/Graphics/AsyncGPUReadbackManaged.h")]
  internal struct AsyncRequestNativeArrayData
  {
    public unsafe void* nativeArrayBuffer;
    public long lengthInBytes;
    public AtomicSafetyHandle safetyHandle;

    public static unsafe AsyncRequestNativeArrayData CreateAndCheckAccess<T>(NativeArray<T> array) where T : struct
    {
      if (array.m_AllocatorLabel == Allocator.Temp || array.m_AllocatorLabel == Allocator.TempJob)
        throw new ArgumentException("AsyncGPUReadback cannot use Temp memory as input since the result may only become available at an unspecified point in the future.");
      AsyncRequestNativeArrayData andCheckAccess = new AsyncRequestNativeArrayData();
      andCheckAccess.nativeArrayBuffer = array.GetUnsafePtr<T>();
      andCheckAccess.lengthInBytes = (long) array.Length * (long) UnsafeUtility.SizeOf<T>();
      AtomicSafetyHandle atomicSafetyHandle = NativeArrayUnsafeUtility.GetAtomicSafetyHandle<T>(array);
      int* versionNode = (int*) (void*) atomicSafetyHandle.versionNode;
      if (atomicSafetyHandle.version != (*versionNode & -6))
        AtomicSafetyHandle.CheckWriteAndThrowNoEarlyOut(atomicSafetyHandle);
      andCheckAccess.safetyHandle = atomicSafetyHandle;
      return andCheckAccess;
    }

    public static unsafe AsyncRequestNativeArrayData CreateAndCheckAccess<T>(NativeSlice<T> array) where T : struct
    {
      if (AtomicSafetyHandle.IsTempMemoryHandle(array.m_Safety))
        throw new ArgumentException("AsyncGPUReadback cannot use Temp memory as input since the result may only become available at an unspecified point in the future.");
      AsyncRequestNativeArrayData andCheckAccess = new AsyncRequestNativeArrayData();
      andCheckAccess.nativeArrayBuffer = array.GetUnsafePtr<T>();
      andCheckAccess.lengthInBytes = (long) array.Length * (long) UnsafeUtility.SizeOf<T>();
      AtomicSafetyHandle atomicSafetyHandle = NativeSliceUnsafeUtility.GetAtomicSafetyHandle<T>(array);
      int* versionNode = (int*) (void*) atomicSafetyHandle.versionNode;
      if (atomicSafetyHandle.version != (*versionNode & -6))
        AtomicSafetyHandle.CheckWriteAndThrowNoEarlyOut(atomicSafetyHandle);
      andCheckAccess.safetyHandle = atomicSafetyHandle;
      return andCheckAccess;
    }
  }
}
